package com.h2.gwt.util.ui.content;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.SimplePager.TextLocation;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.SelectionModel;

public abstract class AContentTable<T> extends Composite {
   
   interface Binder extends UiBinder<Widget, AContentTable<?>> { }

   private static final Binder BINDER = GWT.create(Binder.class);
   
   ////

   /**
    * The main CellTable.
    */
   @UiField(provided = true)
   public CellTable<T> _cellTable;

   /**
    * The _pager used to change the range of data.
    */
   @UiField(provided = true)
   public SimplePager _pager;
   
   ////
   ////

//   public interface CellTableResource extends CellTable.Resources {
//      public interface CellTableStyle extends CellTable.Style {};
//
//      @Source({"CellTable.css"})
//      CellTableStyle cellTableStyle();
//   }; 
   
   public AContentTable() {
      // Create a Pager to control the table.
      SimplePager.Resources pagerResources = GWT
            .create(SimplePager.Resources.class);
      _pager = new SimplePager(TextLocation.CENTER, pagerResources, false, 0,
            true);
      _pager.setDisplay(_cellTable);

//      CellTableResource resource = GWT.create(CellTableResource.class); 
      // Create a CellTable.
      _cellTable = new CellTable<T>(_pager.getPageCount());

      // Add a selection model so we can select cells.
      final MultiSelectionModel<T> selectionModel =
         new MultiSelectionModel<T>();
      _cellTable.setSelectionModel(selectionModel);
      
//      _cellTable.setFocus(true);

      // Initialize the columns.
      initTableColumns(selectionModel);

      // Create the UiBinder.
      initWidget(BINDER.createAndBindUi(this));
   }

   protected abstract void initTableColumns(
         SelectionModel<T> selectionModel);

   /**
    * @return the _cellTable
    */
   public CellTable<T> getCellTable() {
      return _cellTable;
   }

   /**
    * @param cellTable 
    * @param _cellTable the _cellTable to set
    */
   public void setCellTable(CellTable<T> cellTable) {
      this._cellTable = cellTable;
   }

}
